package com.cq.hd.product.validator;

import com.cq.hd.common.enums.biz.*;
import com.cq.hd.common.response.Throw;
import com.cq.hd.product.api.dto.ActiveDiscountSonDto;
import com.cq.hd.product.api.dto.ActiveGoodsSonDto;
import com.cq.hd.product.api.dto.ActiveReleaseDto;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * 活动校验类 for 商品
 *
 * @author much
 * @since 2024/05/17 20:50
 */
@Component
public class ActiveProductValidator extends ActivePublishValidator {

    @Override
    Boolean match(ActiveReleaseDto activeReleaseDto) {
        return activeReleaseDto.getActiveType().equals(ActiveTypeEnum.GROUP.getValue())
                || activeReleaseDto.getActiveType().equals(ActiveTypeEnum.GROUP_ENROLL.getValue());
    }

    @Override
    public void doValidate(ActiveReleaseDto activeReleaseDto) {
        // 库存校验
        validateStock(activeReleaseDto);
        // 补贴设置
        validateSubsidyState(activeReleaseDto);
        // 校验活动商品列表
        validateGoods(activeReleaseDto);
        // 多件多折
        validateDiscountInfo(activeReleaseDto);
    }

    // 库存校验
    private void validateStock(ActiveReleaseDto activeReleaseDto) {
        Integer totalStock = activeReleaseDto.getTotalStock();
        if (totalStock == null) {
            Throw.isBusinessException("请填写活动库存");
        }
        if (totalStock <= 0) {
            Throw.isBusinessException("活动库存必须大于0");
        }
        if (totalStock > 99999999) {
            Throw.isBusinessException("活动库存不能超过99999999");
        }
    }

    // 校验补贴设置
    private void validateSubsidyState(ActiveReleaseDto activeReleaseDto) {
        // 补贴设置(0-未设置,1-自动按比例分摊,2-手动设置)
        Integer subsidyState = activeReleaseDto.getSubsidyState();
        if (subsidyState == null || SubsidyStateEnum.typeOf(subsidyState) == null) {
            Throw.isBusinessException("请选择补贴设置");
        }
    }

    // 校验活动商品列表
    private void validateGoods(ActiveReleaseDto activeReleaseDto) {
        List<ActiveGoodsSonDto> activeGoods = activeReleaseDto.getActiveGoods();
        if (org.springframework.util.CollectionUtils.isEmpty(activeGoods)) {
            Throw.isBusinessException("请选择活动商品");
        }
    }

    // 校验多件多折
    private void validateDiscountInfo(ActiveReleaseDto activeReleaseDto) {
        // 多件多折状态(1-关闭，2-开启)
        Integer discountState = activeReleaseDto.getDiscountState();
        if (discountState == null || DiscountStateEnum.typeOf(discountState) == null) {
            Throw.isBusinessException("请选择多件多折开启状态");
        }

        List<ActiveDiscountSonDto> activeDiscount = activeReleaseDto.getActiveDiscount();
        if (DiscountStateEnum.OPEN.getValue().equals(discountState)) {
            if (ZeroStateEnum.YES.getValue().equals(activeReleaseDto.getZeroState())) {
                Throw.isBusinessException("零元购活动不能开启多件多折");
            }

            if (activeReleaseDto.getActivePrice().compareTo(new BigDecimal("1")) < 0) {
                Throw.isBusinessException("活动价小于1元,不能开启多件多折");
            }

            if (CollectionUtils.isEmpty(activeDiscount)) {
                Throw.isBusinessException("请设置多件多折");
            }

            for (ActiveDiscountSonDto activeDiscountSonDto : activeDiscount) {
                Integer num = activeDiscountSonDto.getNum();
                if (num <= 0) {
                    Throw.isBusinessException("多件多折的件数必须大于0");
                }
                // 多件多折的打折限制在0.1-10之间
                BigDecimal discount = activeDiscountSonDto.getDiscount();
                if (discount.compareTo(new BigDecimal("0.1")) <= 0 || discount.compareTo(new BigDecimal("10")) >= 0) {
                    Throw.isBusinessException("多件多折的折扣限制在0.1~10之间");
                }
            }
        }
    }

//    private void validateCommissionAmt(ActiveReleaseDto activeReleaseDto) {
//        if (!AgentStateEnum.OPEN.getValue().equals(activeReleaseDto.getActiveState())
//                || !DefaultCommissionTypeEnum.COMMISSION.getValue().equals(activeReleaseDto.getDefaultCommissionType())) {
//            return;
//        }
//
//        // 固定佣金不能大于等于活动价格
//        if (activeReleaseDto.getCommissionAmt().compareTo(activeReleaseDto.getActivePrice()) >= 0) {
//            Throw.isBusinessException("固定金额不能大于等于活动价格");
//        }
//    }

}
